home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Comms / Digester / sources / digester.c dialog < prev    next >
Encoding:
Text File  |  1994-08-23  |  7.4 KB  |  347 lines  |  [TEXT/ttxt]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*
  5. #define    DEBUG    1
  6. */
  7. #define    BOD    "Info-Mac Digest      "
  8. #define    EOD    "End of Info-Mac Digest"
  9. #define    BOT    "Today's Topics:"
  10. #define    EOT    "--------------------------------------------------------------------"
  11. #define    EOM    "------------------------------\n"
  12. #define    BOF    "[Archived as "
  13. #define    FTP    "\nFetch from <a href=ftp://sumex-aim.stanford.edu%s>SUMEX</a>"
  14. #define    FUN    ", \n<a href=ftp://src.doc.ic.ac.uk/computing/systems/mac%s>UK</a>"
  15. #define    USA    ", \n<a href=ftp://ftp.hawaii.edu/mirrors%s>Hawaii</a>"
  16. #define    JAP    ", \n<a href=ftp://ftp.u-tokyo.ac.jp/pub%s>U-Tokyo</a>"
  17. #define    CAN    ", \n<a href=ftp://ftp.ucs.ubc.ca/pub/mac%s>Canada</a>"
  18. #define    NED    ", \n<a href=ftp://ftp.fenk.wau.nl/pub/mac%s>The Netherlands</a>"
  19. #define    SWD    ", \n<a href=ftp://ftp.sunet.se/mac%s>Sweden</a>"
  20. #define    MOR    10     /* maximum items overrun */
  21. #define    DRE    "http://dutera.et.tudelft.nl/people/vdham/vdham.html"
  22.  
  23. FILE    *outP, *inP;
  24.  
  25. #ifdef THINKC
  26. #define REMOVE_ALL_EVENTS        0
  27. #define MY_PROMPT                 "\pChoose INFOMAC e-mail file:"
  28. #define NIL_FILE_FILTER            NULL
  29. #define NIL_DIALOG_HOOK            NULL
  30.  
  31. /******************************    GetFileName *******/
  32. GetFileName( replyPtr )
  33. SFReply        *replyPtr;
  34. {
  35.     Point        myPoint;
  36.     SFTypeList    typeList;
  37.     int            numTypes;
  38.     
  39.     myPoint.h = 100;
  40.     myPoint.v = 100;
  41.     typeList[ 0 ] = 'TEXT';
  42.     numTypes = 1;
  43.     
  44.     SFGetFile( myPoint, MY_PROMPT, NIL_FILE_FILTER, numTypes,
  45.                 &typeList, NIL_DIALOG_HOOK, replyPtr );
  46. }
  47.  
  48. #endif
  49.  
  50. int ParseOne(int buttonsOn)
  51. {
  52.     char    line[512], tempLine[512];
  53.     int    num, i, l;
  54.     int    tocItem = 0;
  55.     int    msgItem = 0;
  56.     int    found;
  57.     char    name[256];
  58.     char    *cptr;
  59.         
  60.     /* find issue */
  61.  
  62.     printf("Parsing...\n");
  63.             
  64.     found = 0;
  65.     
  66.     do {
  67.             if( fgets( line, 256, inP ) != NULL )
  68.             {
  69.                 if( strstr( line, BOD ) != NULL )
  70.                 {
  71.                     printf("Found %s\n", BOD);
  72.                     printf(line);
  73.                     found = 1;
  74.                     cptr = (char *)strstr( line, "Issue" );
  75.                     if( cptr==NULL )
  76.                     {
  77.                         printf("Can't find issue number\n");
  78.                         return(0);
  79.                     }
  80.                     num = atoi( cptr+6 );                
  81.                     sprintf(name, "issue%d.html", num);
  82.                     printf("name = '%s'\n", name);
  83.                     if( (outP = fopen( name,"w")) == NULL )
  84.                     {
  85.                         printf("Can't open '%s'\n", name );
  86.                         return(0);
  87.                     }
  88.                 }
  89.             }
  90.             else
  91.                 return(0);
  92.     } while( !found );
  93.  
  94. #ifdef DEBUG
  95.     printf("Title found\n");
  96. #endif
  97.  
  98.     fprintf(outP,"<TITLE>Issue %d</TITLE>\n", num);
  99.     fprintf(outP,"<pre>\n");
  100.     fprintf(outP,"%s\n",line);
  101.  
  102.     /* find toc */
  103.     
  104.     found = 0;
  105.     
  106.     do {
  107.             if( fgets( line, 256, inP ) != NULL )
  108.             {
  109.                 if( strstr( line, BOT ) != NULL )
  110.                 {
  111.                     found = 1;
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 fclose( outP );
  117.                 return(0);
  118.             }
  119.     } while( !found );
  120.  
  121.  
  122. #ifdef DEBUG
  123.     printf("TOC found\n");
  124. #endif
  125.  
  126.     line[strlen(line)-1] = 0;
  127.     fprintf(outP, "<a name=toc>%s</a>\n", line);
  128.  
  129.     fgets( line, 256, inP ); /* get empty line */
  130.     fputs("<ul>\n", outP);
  131.         
  132.     while( strlen(fgets( line, 256, inP ))>1 )
  133.     {
  134.         i=0;
  135.         while( i<strlen(line) && line[i]==' ' ) i++;
  136.         if( i==strlen(line) ) i=0;
  137.         
  138.         tocItem++;
  139.         line[strlen(line)-1]=0;    /* terminate line with 0 */
  140.         fprintf(outP, "<li><a name=toc%d> </a><a href=#item%d>%s</a>\n",
  141.                     tocItem, tocItem, line+i);
  142.     }    
  143.  
  144.     fprintf(outP, "</ul>\n\n\n");
  145.     
  146.     while( strstr( fgets(line, 256, inP), EOT ) == NULL )
  147.         fputs( line, outP );
  148.  
  149.     fputs( line, outP );
  150.  
  151.     found = 0;
  152.     
  153.     do{
  154.         msgItem++;
  155.         
  156.         fprintf( outP, "<a name=item%d> </a>",msgItem);
  157.  
  158.         if( buttonsOn )
  159.         {
  160.             fprintf( outP, "<a href=#item%d><img src=next.gif alt=Next></a> ", msgItem+1);
  161.             if( msgItem>tocItem )
  162.                 fprintf( outP, "<a href=#toc><img src=toc.gif alt=TOC></a>\n");
  163.             else
  164.                 fprintf( outP, "<a href=#toc%d><img src=toc.gif alt=TOC></a>\n", msgItem);
  165.         }
  166.         else
  167.         {
  168.             fprintf( outP, "<a href=#item%d>Next</a> ", msgItem+1);
  169.             if( msgItem>tocItem )
  170.                 fprintf( outP, "<a href=#toc>TOC</a>\n");
  171.             else
  172.                 fprintf( outP, "<a href=#toc%d>TOC</a>\n", msgItem);
  173.         }
  174.         
  175.         while( strcmp( fgets( line, 256, inP ), EOM )!=0 &&
  176.                  strstr( line, EOD ) == NULL && !feof(inP) )
  177.         {
  178.             /* filter entities in mail */
  179.             
  180.             tempLine[0]=0;
  181.             l = 0;
  182.             for(i=0; i<strlen(line); i++ )
  183.             {
  184.                 if( line[i]=='<' )
  185.                 {
  186.                     tempLine[l++] = '&';
  187.                     tempLine[l++] = 'l';
  188.                     tempLine[l++] = 't';
  189.                     tempLine[l++] = ' ';
  190.                 }
  191.                 else
  192.                 if( line[i]=='>' )
  193.                 {
  194.                     tempLine[l++] = '&';
  195.                     tempLine[l++] = 'g';
  196.                     tempLine[l++] = 't';
  197.                     tempLine[l++] = ' ';
  198.                 }
  199.                 else
  200.                 if( line[i]=='"' )
  201.                 {
  202.                     tempLine[l++] = '&';
  203.                     tempLine[l++] = 'q';
  204.                     tempLine[l++] = 'u';
  205.                     tempLine[l++] = 'o';
  206.                     tempLine[l++] = 't';
  207.                     tempLine[l++] = ' ';
  208.                 }
  209.                 else
  210.                 if( line[i]=='&' )
  211.                 {
  212.                     tempLine[l++] = '&';
  213.                     tempLine[l++] = 'a';
  214.                     tempLine[l++] = 'm';
  215.                     tempLine[l++] = 'p';
  216.                     tempLine[l++] = ' ';
  217.                 }
  218.                 else
  219.                 {
  220.                     tempLine[l++] = line[i];
  221.                     tempLine[l] = 0;
  222.                 }
  223.             }
  224.             strcpy( line, tempLine );
  225.             
  226.             fputs(line, outP);
  227.             
  228.             if( (cptr=strstr( line, "/info-mac/" )) != NULL )
  229.             {
  230.                 num = cptr-line;
  231.                 i = 0;
  232.                 while( line[num] != ';' && num<strlen(line) && i<256 )
  233.                 {
  234.                     name[i]=line[num];
  235.                     i++;
  236.                     num++;
  237.                 }
  238.                 name[i] = 0;
  239.                 line[strlen(line)-1]=0;
  240.                 fprintf( outP, "\n</pre>" );
  241.                 
  242.                 fprintf( outP, FTP, name);            
  243.                 fprintf( outP, FUN, name);            
  244.                 fprintf( outP, USA, name);            
  245.                 fprintf( outP, JAP, name);            
  246.                 fprintf( outP, CAN, name);            
  247.                 fprintf( outP, NED, name);            
  248.                 fprintf( outP, SWD, name);            
  249.                 
  250.                 fprintf( outP, ".\n<pre>\n" );
  251.             }
  252.         }
  253.         fputs( line, outP );
  254.                     
  255.     }while( strstr( line, EOD ) == NULL && msgItem<(tocItem+MOR) && !feof( inP ) );
  256.     
  257.     fprintf( outP, "</pre>\n" );
  258.     fputs("<p><hr><p>\n", outP);
  259.     fprintf( outP, "<a name=item%d> </a>",msgItem+1);
  260.     fputs("HTML file created by digester<p>\n", outP);
  261.     fprintf(outP,"Digester developed by: <a href=%s>André  C. van der Ham</a><p>\n", DRE);
  262.     fputs("<address>\n", outP);
  263.     fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP);
  264.     fputs("</address>\n", outP);    
  265.  
  266.     fclose( outP );
  267.     
  268.     printf("file closed.\n");
  269.      
  270.     if( feof( inP ) )
  271.         printf("Premature end of file found on input file.\n");
  272.         
  273.     if( msgItem != tocItem )
  274.         printf("!! msgItem = %d, tocItem = %d\n", msgItem, tocItem );
  275.         
  276.     return(1);
  277. }
  278.  
  279. int main( void )
  280. {
  281.     char    name[80];
  282.     char    line[256];
  283.     int        i;
  284.  
  285. #ifdef THINKC
  286.     SFReply    reply;
  287. #endif
  288.  
  289.     int    okay=1;
  290.  
  291.     puts("\n\n*** D I G E S T E R ***\n");
  292.     puts("by Andre' C. van der Ham\n");
  293.     puts("This program parses info-mac digests you get by e-mail.");
  294.     puts("Save the e-mail without headers and paragraph recognition (Eudora)");
  295.  
  296. #ifndef THINKC
  297.     puts("Convert file to appropriate ascii format, for example: Mac->Unix\n");
  298. #endif
  299.  
  300.     puts("The file can contain a number of digests.");
  301.     puts("An HTML file is generated for each issue.");
  302.     puts("Send bug reports, questions, etc... to:");
  303.     puts("e-mail: A.C.vanderHam@ET.TUDelft.NL\n\n");
  304.  
  305. #ifndef THINKC
  306.     printf("Input file: ");        
  307.     gets( name );
  308. #endif
  309.  
  310. #ifdef THINKC
  311.     puts("WARNING: this program only works if the DIGESTER program and the e-mail file");
  312.     puts("are in the same folder (sorry).");
  313.     puts("");
  314.     puts("Click mouse to continue.");
  315.     while(!Button());
  316.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  317.  
  318.     GetFileName( &reply );
  319.     
  320.     if( reply.good!=TRUE )
  321.     {
  322.         return(1);
  323.     }
  324.     
  325.     for(i=0;i<reply.fName[0]; i++ )
  326.         name[i]=reply.fName[i+1];
  327.     
  328.     name[i]=0;
  329. #endif
  330.     
  331.     if( (inP = fopen( name, "r" )) != NULL )
  332.     {
  333.         while( okay )
  334.         {
  335.             okay = ParseOne(1);
  336.             
  337. #ifdef DEBUG
  338.             printf("%d: next...\n", okay);
  339. #endif
  340.  
  341.         }
  342.  
  343.         fclose( inP );
  344.     }
  345.     
  346.     return(0);
  347. }